//
//  ZLAlert.m
//  ZLAlert
//
//  Created by  Jiaozl on 2017/3/31.
//  Copyright © 2017年  Jiaozl. All rights reserved.
//


#import "ZLAlert.h"

@implementation ZLAlert

+ (void)showSimpleAlertWithTitle:(NSString *)title
                         message:(NSString *)message
                     cancleTitle:(NSString *)cancelTitle
                    cancleHandle:(AlertCancelHandle)cancleHandle
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title
                                                                     message:message
                                                              preferredStyle: UIAlertControllerStyleAlert];
    
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:cancelTitle
                                                               style:UIAlertActionStyleCancel
                                                             handler:^(UIAlertAction * _Nonnull action) {
                                                                 if (cancleHandle) {
                                                                     cancleHandle();
                                                                 }
                                                             }];
    
    [alertVC addAction:cancleAction];
    
    [[ZLAlert currentViewController] presentViewController:alertVC animated:YES completion: nil];
}

+ (void)showAlertWithTitle:(NSString *)title
                   message:(NSString *)message
              confirmTitle:(NSString *)confirmTitle
               cancelTitle:(NSString *)cancelTitle
            preferredStyle:(UIAlertControllerStyle)preferredStyle
             confirmHandle:(AlertConfirmHandle)confirmHandle
              cancleHandle:(AlertCancelHandle)cancleHandle {
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title
                                                                     message:message
                                                              preferredStyle: preferredStyle];
    
    if (cancelTitle != nil) {
        UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:cancelTitle
                                                               style:preferredStyle == UIAlertControllerStyleAlert ? UIAlertActionStyleDefault : UIAlertActionStyleCancel
                                                             handler:^(UIAlertAction * _Nonnull action) {
                                                                 if (cancleHandle) {
                                                                     cancleHandle();
                                                                 }
                                                             }];
        [alertVC addAction:cancleAction];
    }
    


    if (confirmTitle != nil) {
        UIAlertAction *confirAction = [UIAlertAction actionWithTitle:confirmTitle
                                                               style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * _Nonnull action) {
                                                                 if (confirmHandle) {
                                                                     confirmHandle();
                                                                 }
                                                             }];
        [alertVC addAction:confirAction];
    }

     [[ZLAlert currentViewController] presentViewController:alertVC animated:YES completion: nil];
}

+ (void)showAlertWithTitle:(NSString *)title
                   message:(NSString *)message
              confirmTitle:(NSString *)confirmTitle
               cancelTitle:(NSString *)cancelTitle
         otherConfirmTitle:(NSString *)otherConfirmTitle
            preferredStyle:(UIAlertControllerStyle)preferredStyle
             confirmHandle:(AlertConfirmHandle)confirmHandle
              cancleHandle:(AlertCancelHandle)cancleHandle
         otherConfirmHandle:(AlertDestructiveHandle)otherConfirmHandle {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title
                                                                     message:message
                                                              preferredStyle: preferredStyle];
    
    if (cancelTitle != nil) {
        UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:cancelTitle
                                                               style:preferredStyle == UIAlertControllerStyleAlert ? UIAlertActionStyleDefault : UIAlertActionStyleCancel
                                                             handler:^(UIAlertAction * _Nonnull action) {
                                                                 if (cancleHandle) {
                                                                     cancleHandle();
                                                                 }
                                                             }];
        [alertVC addAction:cancleAction];
    }
    
    
    
    if (confirmTitle != nil) {
        UIAlertAction *confirAction = [UIAlertAction actionWithTitle:confirmTitle
                                                               style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * _Nonnull action) {
                                                                 if (confirmHandle) {
                                                                     confirmHandle();
                                                                 }
                                                             }];
        [alertVC addAction:confirAction];
    }
    
    if (otherConfirmTitle != nil) {
        UIAlertAction *confirAction = [UIAlertAction actionWithTitle:otherConfirmTitle
                                                               style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * _Nonnull action) {
                                                                 if (otherConfirmHandle) {
                                                                     otherConfirmHandle();
                                                                 }
                                                             }];
        [alertVC addAction:confirAction];
    }
    
    [[ZLAlert currentViewController] presentViewController:alertVC animated:YES completion: nil];
}

+ (void)showAlertWithTitle:(NSString *)title
                   message:(NSString *)message
            preferredStyle:(UIAlertControllerStyle)preferredStyle
               actionMaker:(AlertMarker)actionMaker {
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title
                                                                     message:message
                                                              preferredStyle: preferredStyle];
    __weak typeof(alertVC) weakAlert = alertVC;
    if (actionMaker) {
        actionMaker(weakAlert);
    }
    
    [[ZLAlert currentViewController] presentViewController:alertVC animated:YES completion:^{
        if (actionMaker == nil || alertVC.actions.count <= 0) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakAlert dismissViewControllerAnimated:YES completion:nil];
            });
        }
    }];}


+ (void)showAlertWithTitle:(NSString *)title
                   message:(NSString *)message
            preferredStyle:(UIAlertControllerStyle)preferredStyle
           autoDismissTime:(int)autoDismissTime {
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title
                                                                     message:message
                                                              preferredStyle: preferredStyle];
    __weak typeof(alertVC) weakAlert = alertVC;
    [[ZLAlert currentViewController] presentViewController:alertVC animated:YES completion:^{
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(autoDismissTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakAlert dismissViewControllerAnimated:YES completion:nil];
            });
    }];
}

+ (void)showAlertWithTitle:(NSString *)title message:(NSString *)msg preferredStyle:(UIAlertControllerStyle)alertCStyle cancelTitle:(NSString *)cancelTitle actionTitles:(NSArray *)titles completeHandler:(void(^)(NSInteger index, NSString *title))completeHandler cancelHandler:(void(^)(void))cancelHandler {
    
    if (titles && titles.count > 0) {
    
        [ZLAlert showAlertWithTitle:title message:msg preferredStyle:alertCStyle actionMaker:^(UIAlertController *maker) {
            
            for (int i = 0; i < titles.count; i++) {
                [maker addAlertActionWithTitle:titles[i] actionStyle:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nullable action) {
                    
                    if (completeHandler) {
                        
                        completeHandler(i, titles[i]);
                    }
                }];
            }
        
            if (cancelTitle != nil) {
                [maker addAlertActionWithTitle:cancelTitle actionStyle:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nullable action) {
                    
                    if (cancelHandler) {
                        
                        cancelHandler();
                    }
                }];
            }
        }];
    }
}

// find currentViewController
+ (UIViewController *)currentViewController {
    UIWindow *window = [[[UIApplication sharedApplication] windows] firstObject];
    UIViewController *presentedVC = [[window rootViewController] presentedViewController];
    if (presentedVC) {
        return presentedVC;
        
    } else {
        return window.rootViewController;
    }
}


@end
